package com.example.audiorecorddemo.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.audiorecorddemo.R;


/**
 * 权限请求弹框
 */
public class PermissionDialog extends Dialog implements View.OnClickListener {
    private TextView mTvTitle, mTvTip, mTvLeftBtn, mTvRightBtn;
    private View mLineTip;

    private Window mWindow;

    private String mTileStr, mTipStr;
    private int mBtnSelectType;
    private String mLeftStr, mRightStr;

    private DialogTipLeftClickListener mLeftClickListener;
    private DialogTipRightClickListener mRightClickListener;
    // 左右button显示
    public static final int BUTTON_BOTH_FLAG = 1;
    // 仅左button显示
    public static final int BUTTON_LEFT_FLAG = 2;
    // 仅右button显示
    public static final int BUTTON_RIGHT_FLAG = 3;


    private boolean needShowCheck = false;

    public PermissionDialog(Context context, String titleStr, String tipStr, int buttonType, String leftStr,
                            String rightStr, DialogTipLeftClickListener leftClickListener,
                            DialogTipRightClickListener rightClickListener) {
        super(context);
        mWindow = getWindow();
        mTileStr = titleStr;
        mTipStr = tipStr;
        mBtnSelectType = buttonType;
        mLeftStr = leftStr;
        mRightStr = rightStr;
        mLeftClickListener = leftClickListener;
        mRightClickListener = rightClickListener;
    }

    public PermissionDialog(Context context, String tipStr, int buttonType, String leftStr,
                            String rightStr, DialogTipLeftClickListener leftClickListener,
                            DialogTipRightClickListener rightClickListener) {
        this(context, "", tipStr, buttonType, leftStr, rightStr, leftClickListener, rightClickListener);
    }

    public PermissionDialog(Context context, String tipStr, String leftStr, String rightStr,
                            DialogTipLeftClickListener leftClickListener,
                            DialogTipRightClickListener rightClickListener) {
        this(context, "", tipStr, BUTTON_BOTH_FLAG, leftStr, rightStr, leftClickListener, rightClickListener);
    }

    public PermissionDialog(Context context, String tipStr, String rightStr,
                            DialogTipLeftClickListener leftClickListener,
                            DialogTipRightClickListener rightClickListener) {
        this(context, "", tipStr, BUTTON_BOTH_FLAG, "", rightStr, leftClickListener, rightClickListener);
    }

    public PermissionDialog(Context context, String tipStr, DialogTipLeftClickListener leftClickListener,
                            DialogTipRightClickListener rightClickListener) {
        this(context, "", tipStr, BUTTON_BOTH_FLAG, "", "", leftClickListener, rightClickListener);
    }

    public PermissionDialog(Context context, String titleStr, String tipStr, int buttonType, String leftStr,
                            String rightStr, DialogTipLeftClickListener leftClickListener,
                            DialogTipRightClickListener rightClickListener, boolean needShowCheck) {
        this(context, titleStr, tipStr, buttonType, leftStr, rightStr, leftClickListener, rightClickListener);
        this.needShowCheck = needShowCheck;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_permiss_dialog);
        setCancelable(false);
        initView();
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_dialog_corner_title);
        mTvTip = findViewById(R.id.tv_dialog_corner_tip);
        mLineTip = findViewById(R.id.view_dialog_corner_line);
        mTvLeftBtn = findViewById(R.id.tv_dialog_corner_left);
        mTvRightBtn = findViewById(R.id.tv_dialog_corner_right);


        // 初始化监听
        initListener();
        // 初始化数据
        initData();
    }

    private void initData() {
        // title
        if (!TextUtils.isEmpty(mTileStr)) {
            mTvTitle.setText(mTileStr);
            mTvTitle.setVisibility(View.VISIBLE);
        } else {
            mTvTitle.setVisibility(View.GONE);
        }
        // 提示
        if (!TextUtils.isEmpty(mTipStr)) {
            mTvTip.setText(mTipStr);
            mTvTip.setVisibility(View.VISIBLE);
        } else {
            mTvTip.setVisibility(View.GONE);
        }
        // 左边按钮
        if (!TextUtils.isEmpty(mLeftStr)) {
            mTvLeftBtn.setText(mLeftStr);
        }
        // 右边按钮
        if (!TextUtils.isEmpty(mRightStr)) {
            mTvRightBtn.setText(mRightStr);
        }
        // 按钮状态
        setButtonSelect(mBtnSelectType);

    }

    private void initListener() {
        mTvLeftBtn.setOnClickListener(this);
        mTvRightBtn.setOnClickListener(this);
    }

    private void setButtonSelect(int selectType) {
        if (mTvLeftBtn == null || mTvRightBtn == null || mLineTip == null) {
            return;
        }
        switch (selectType) {
            case BUTTON_LEFT_FLAG:
                mTvLeftBtn.setVisibility(View.VISIBLE);
                mTvRightBtn.setVisibility(View.GONE);
                mLineTip.setVisibility(View.GONE);
                break;
            case BUTTON_RIGHT_FLAG:
                mTvLeftBtn.setVisibility(View.GONE);
                mTvRightBtn.setVisibility(View.VISIBLE);
                mLineTip.setVisibility(View.GONE);
                break;
            case BUTTON_BOTH_FLAG:
            default:
                mTvLeftBtn.setVisibility(View.VISIBLE);
                mTvRightBtn.setVisibility(View.VISIBLE);
                mLineTip.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (R.id.tv_dialog_corner_left == viewId) {
            if (mLeftClickListener != null) {
                mLeftClickListener.onTipLeftClick();
            }
            if (isShowing()) {
                dismiss();
            }
        } else if (R.id.tv_dialog_corner_right == viewId) {
            if (mRightClickListener != null) {
                mRightClickListener.onTipRightClick();
            }
            if (isShowing()) {
                dismiss();
            }
        }
    }

    public interface DialogTipLeftClickListener {
        void onTipLeftClick();
    }

    public interface DialogTipRightClickListener {
        void onTipRightClick();
    }
}
